package com.phai.srtfix.main;


import com.phai.srtfix.util.Fix;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import org.jvnet.substance.skin.SubstanceCremeCoffeeLookAndFeel;
import sun.swing.ImageIconUIResource;



/*
 * Main.java
 *
 * Created on Feb 6, 2010, 6:59:28 AM
 */
/**
 *
 * @author fahri.cyber
 */
public class Main extends javax.swing.JFrame {

    private File fileInput;
    private File fileOutput;

    private static final String INFO_INPUT = "Choose a .srt file ...";
    private static final String INFO_OUTPUT = "Choose output of file ...";
   
    /** Creates new form Main */
    public Main() {
        //System.setProperty("Quaqua.tabLayoutPolicy", "wrap");

        // set the Quaqua Look and Feel in the UIManager
        try {
           // UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");

            UIManager.setLookAndFeel(new SubstanceCremeCoffeeLookAndFeel());
            //  Font f3= new Font("Times New Roman",Font.BOLD,15);
            // this.setFont(f3);
           
            setIconImage(ImageIO.read(this.getClass().getResource("/icon/logo.png")));
            
            

        } catch (Exception e) {
            System.out.println(e);
        }
        initComponents();
        tfSecond.setModel(new SpinnerNumberModel(new Double("00.000"), new Double("00.000"), new Double("60.000"), new Double("0.1")));
        tfMinute.setModel(new SpinnerNumberModel(new Integer("00"), new Integer("0"), new Integer("60"), new Integer("1")));
        tfHour.setModel(new SpinnerNumberModel(new Integer("00"), new Integer("0"), new Integer("24"), new Integer("1")));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        butgroupPilih = new javax.swing.ButtonGroup();
        tfInput = new javax.swing.JTextField();
        butInput = new javax.swing.JButton();
        tfOutput = new javax.swing.JTextField();
        butOutput = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        radioMaju = new javax.swing.JRadioButton();
        radioMundur = new javax.swing.JRadioButton();
        lblSign = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        butFix = new javax.swing.JButton();
        tfHour = new javax.swing.JSpinner();
        tfMinute = new javax.swing.JSpinner();
        tfSecond = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SRTFix");

        tfInput.setText("Choose a .srt file ...");
        tfInput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tfInputMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tfInputMouseExited(evt);
            }
        });

        butInput.setText("Input ...");
        butInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butInputActionPerformed(evt);
            }
        });

        tfOutput.setText("Choose output of file ...");
        tfOutput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tfOutputMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tfOutputMouseExited(evt);
            }
        });

        butOutput.setText("Output ...");
        butOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butOutputActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        butgroupPilih.add(radioMaju);
        radioMaju.setText("Maju");
        radioMaju.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioMajuActionPerformed(evt);
            }
        });

        butgroupPilih.add(radioMundur);
        radioMundur.setSelected(true);
        radioMundur.setText("Mundur");
        radioMundur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioMundurActionPerformed(evt);
            }
        });

        lblSign.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        lblSign.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblSign.setText("-");

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel1.setText(":");

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText(":");

        butFix.setText("Fix Subtitle!");
        butFix.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butFixActionPerformed(evt);
            }
        });

        jLabel3.setText("H");

        jLabel4.setText("M");

        jLabel5.setText("S");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(lblSign, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfHour, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel3)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel4)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfSecond, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(butFix))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel5)))
                .addGap(44, 44, 44))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addComponent(radioMaju)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(radioMundur)
                    .addGap(201, 201, 201)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSign)
                    .addComponent(tfHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(butFix, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(tfSecond, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jLabel5))
                    .addComponent(jLabel3))
                .addContainerGap(16, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(15, 15, 15)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(radioMaju)
                        .addComponent(radioMundur))
                    .addContainerGap(85, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(butOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(butInput, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfInput, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tfInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(butInput)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tfOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(butOutput)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfInputMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfInputMouseEntered
        // TODO add your handling code here:
        if (tfInput.getText().equals(INFO_INPUT)) {
            tfInput.setText("");
        }
    }//GEN-LAST:event_tfInputMouseEntered

    private void tfInputMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfInputMouseExited
        // TODO add your handling code here:
        if (tfInput.getText().equals("")) {
            tfInput.setText(INFO_INPUT);
        }
    }//GEN-LAST:event_tfInputMouseExited

    private void tfOutputMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfOutputMouseEntered
        
        if (tfOutput.getText().equals(INFO_OUTPUT)) {
            tfOutput.setText("");
        }
    }//GEN-LAST:event_tfOutputMouseEntered

    private void tfOutputMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfOutputMouseExited
        // TODO add your handling code here:
        if (tfOutput.getText().equals("")) {
            tfOutput.setText(INFO_OUTPUT);
        }
    }//GEN-LAST:event_tfOutputMouseExited

    private void radioMajuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioMajuActionPerformed
        // TODO add your handling code here:
        lblSign.setText("+");
    }//GEN-LAST:event_radioMajuActionPerformed

    private void radioMundurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioMundurActionPerformed
        // TODO add your handling code here:
        lblSign.setText("-");
    }//GEN-LAST:event_radioMundurActionPerformed

    private void butFixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butFixActionPerformed
        // TODO add your handling code here:
        
        Double second = (Double) tfSecond.getValue();
        Integer minute = (Integer) tfMinute.getValue();
        Integer hour = (Integer) tfHour.getValue();

        System.out.println("State : " + (radioMaju.isSelected()? "+" : "-"));
        System.out.println("HOUR : " + hour);
        System.out.println("MINUTE : " + minute);
        System.out.println("SECOND : " + second);

        if(!tfInput.getText().equals(INFO_INPUT))
        {
             fileInput = new File(tfInput.getText());
        }
       

        if(!tfOutput.getText().equals(INFO_OUTPUT))
        {
            fileOutput = new File(tfOutput.getText());
        }
        

        
        

        if(fileInput == null || fileOutput == null)
        {
            System.out.println("Ada yang null");
            JOptionPane.showMessageDialog(this, "Pilih input file dan output file !", "Error !", JOptionPane.ERROR_MESSAGE, new ImageIconUIResource(this.getIconImage()));
            
        }
        else if(hour > 24 || minute > 60 || second > 60)
        {
            System.out.println("Error !");
            
            JOptionPane.showMessageDialog(this, "Hour tidak boleh lebih dari 24\nMinute tidak boleh lebih dari 60\nSecond tidak boleh lebih dari 60", "Error !", JOptionPane.ERROR_MESSAGE, new ImageIconUIResource(this.getIconImage()));

        }
        else
        {
            System.out.println("Buat fixSubtitle");
            Fix fixSubtitle  = new Fix(fileInput, fileOutput, radioMaju.isSelected(), hour, minute, second);

            if(fixSubtitle.fixSrt())
            {
                JOptionPane.showMessageDialog(this, "Siap pakai !", "Berhasil !", JOptionPane.INFORMATION_MESSAGE, new ImageIconUIResource(this.getIconImage()));

            }
            else
            {
                JOptionPane.showMessageDialog(this, "GAGAL fiksasi !", "Error !", JOptionPane.ERROR_MESSAGE, new ImageIconUIResource(this.getIconImage()));

            }
            
        }

        
    }//GEN-LAST:event_butFixActionPerformed

    private void butInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butInputActionPerformed
        // TODO add your handling code here:

        final JFileChooser fc = new JFileChooser();

        int returnVal = fc.showOpenDialog(this);

       

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileInput = fc.getSelectedFile();
            
            tfInput.setText(fileInput.getAbsolutePath());
            tfOutput.setText(fileInput.getAbsolutePath() + ".fix");
            
        } else {
            System.out.println("INPUT FILE gagal: " );
        }


    }//GEN-LAST:event_butInputActionPerformed

    private void butOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butOutputActionPerformed
        // TODO add your handling code here:
        final JFileChooser fc = new JFileChooser();

        int returnVal = fc.showOpenDialog(this);

         if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileOutput = fc.getSelectedFile();

            //This is where a real application would open the file.
            //tfInput.setText(fileInput.getAbsolutePath());
            tfOutput.setText(fileOutput.getAbsolutePath());
            
        } else {
            System.out.println("INPUT FILE gagal: " );
        }
    }//GEN-LAST:event_butOutputActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butFix;
    private javax.swing.JButton butInput;
    private javax.swing.JButton butOutput;
    private javax.swing.ButtonGroup butgroupPilih;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblSign;
    private javax.swing.JRadioButton radioMaju;
    private javax.swing.JRadioButton radioMundur;
    private javax.swing.JSpinner tfHour;
    private javax.swing.JTextField tfInput;
    private javax.swing.JSpinner tfMinute;
    private javax.swing.JTextField tfOutput;
    private javax.swing.JSpinner tfSecond;
    // End of variables declaration//GEN-END:variables
}
